package com.qrxl.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qrxl.common.exception.CustomException;
import com.qrxl.util.JGAssert;
import com.qrxl.common.vo.BaseQuery;
import com.qrxl.common.vo.req.IdReq;
import com.qrxl.enums.EnumRoleType;
import com.qrxl.user.entity.Role;
import com.qrxl.user.entity.RoleMenu;
import com.qrxl.user.entity.UserRole;
import com.qrxl.user.mapper.RoleMapper;
import com.qrxl.user.service.IRoleMenuService;
import com.qrxl.user.service.IRoleService;
import com.qrxl.user.service.IUserRoleService;
import com.qrxl.user.vo.UserLoginInfo;
import com.qrxl.user.vo.req.RoleAddReq;
import com.qrxl.user.vo.req.RoleUpdateReq;
import com.qrxl.user.vo.res.RoleListRes;
import com.qrxl.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author friday
 * @since 2020-12-05
 */
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {


    @Autowired
    IRoleMenuService roleMenuService;

    @Autowired
    IUserRoleService userRoleService;

    @Override
    public void add(RoleAddReq req) {
        Role record = new Role();
        BeanUtils.copyProperties(req, record);
        getBaseMapper().insert(record);
    }

    @Override
    public IPage<RoleListRes> selectPage(BaseQuery baseQuery, Role req) {
        QueryWrapper wrappers = StringUtil.buildBaseQuery(new Role(), baseQuery, req);

        List<RoleListRes> resultList = new ArrayList<>();
        IPage page = getBaseMapper().selectPage(new Page(baseQuery.getPage(), baseQuery.getPageSize()), wrappers);
        page.getRecords().forEach(one -> {
            RoleListRes record = new RoleListRes();
            BeanUtils.copyProperties(one, record);
            record.setTypeName(EnumRoleType.getType(record.getType()).getName());
            resultList.add(record);
        });
        page.setRecords(resultList);
        return page;
    }

    @Override
    public void updateVo(RoleUpdateReq req, UserLoginInfo ul) {
        Role role = baseMapper.selectById(req.getId());
        JGAssert.notNull(role, "角色不存在");
        JGAssert.isTrue(ul.getLoginName().equalsIgnoreCase(role.getOperator()), "只能修改自己创建的角色");
        Role updateObj = new Role();
        BeanUtils.copyProperties(req, updateObj);
        updateById(updateObj);
    }

    @Override
    public List<Role> selectListByUserId(String loginId) {

        return getBaseMapper().selectListByUserId(loginId);

    }

    @Override
    @Transactional
    public void delete(IdReq req, UserLoginInfo ul) {
        Role dbRecord = getById(req.getId());
        if (!ul.getLoginName().equalsIgnoreCase(dbRecord.getOperator())) {
            throw new CustomException("只允许删除自己创建的角色");
        }

        UserRole cond = new UserRole();
        cond.setRoleId(req.getId());
        if (userRoleService.count(Wrappers.query(cond)) > 0) {
            throw new CustomException("有用户关联到该角色，不能删除");
        }
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(req.getId());
        log.info("删除角色时删除菜单关联：roleId" + req.getId());
        roleMenuService.remove(Wrappers.query(roleMenu));
        baseMapper.deleteById(req.getId());
    }
}
